package cn.xiaoyuan.controller.page;

import cn.xiaoyuan.dao.ProductMapper;
import cn.xiaoyuan.entity.Pager;
import cn.xiaoyuan.entity.Sell;
import cn.xiaoyuan.service.AdminService;
import cn.xiaoyuan.service.SellService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/page/admin")
@Controller
public class AdminPageController {
    @Resource
    SellService sellService;
    @Resource
    AdminService adminService;
//    @Resource
//    ProductMapper productMapper;
//    public Pager<Sell> findByUserPager(int page, int size){
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("page", (page-1)*size);
//        params.put("size", size);
//        List<Sell> list = productMapper.findSellByPage(params);
//        Pager<Sell> pager = new Pager<Sell>();
//        pager.setData(list);
//        pager.setTotal(productMapper.findUserCount());
//        return pager;
//    }
    @RequestMapping("/adminList")
    public String adminList(Model model){
        model.addAttribute("adminList",adminService.getAdminList());
        return "admin/admin";
    }
    @RequestMapping("/userList")
    public String userList(Model model){
        model.addAttribute("userList",adminService.getUserList());
        return "admin/user";
    }
    @RequestMapping("/orders")
    public ModelAndView order(HttpServletRequest request){
        int pageNum=1;
        if(request.getParameter("pageNum")==null||"".equals(request.getParameter("page"))){
            pageNum=1;
        }else{
            pageNum=Integer.parseInt(request.getParameter("pageNum"));
        }
        System.out.println(pageNum+"====page");
        ModelAndView model=new ModelAndView();
        model.addObject("itemsList",sellService.getSellList(pageNum));
        //model.addObject("ordersList",sellService.getSellList());
        model.setViewName("admin/orders");
        return model;
    }
    @RequestMapping("/vistorList")
    public String vistorList(Model model){
        model.addAttribute("vistorList",adminService.getVistorList());
        return "admin/vistor";
    }

}
